package connect4swt;

import connect4client.core.Controller;
import connect4client.core.GUIMediator;
import connect4swt.gui.*;
import connect4client.net.ConnectionController;
import connect4client.net.servers.Server;

/**
 * @author  Joel Cordeiro
 */
public class ClientApplication {
		
	private static Controller controller = new Controller();
	
	
	/**
	 * @return
	 * @uml.property  name="controller"
	 */
	public static Controller getController() {
		return controller;
	}
	
	/**
	 * Calls the login method from the GuiInteraction class
	 * and tries to autenticate to the server.
	 */
	private static void login(){						
		LoginWindow.getInstance().open();
	}		
	
	public static void main(String[] args) {
		GUIMediator guiMediator = new GUIMediatorImpl(controller);
		controller.setGUIMediator(guiMediator);
		
		if (args.length == 4) {
			try {
				String serverType = args[0].toLowerCase();
				String serverIp = args[1];
				String userName = args[2];
				String password = args[3];
				
				Server server = null;
				ConnectionController.CommunicationMethod method = null;
				if (serverType.equals("rmi")) {
					server = new Server(serverIp, 1099);
					method = ConnectionController.CommunicationMethod.RMI;
				} else if (serverType.equals("socket")) {
					server = new Server(serverIp, 32000);
					method = ConnectionController.CommunicationMethod.TCP;
				}
				
				controller.getGUIMediator().sendLoginMessage(method, server, userName, password);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			login();
		}
	}
}
